/***********************************
Puropose: To update the Manager in the Leads for Lead Owners
Author:   IDC
***********************************/
global class OORUpdateLeads implements Database.Batchable<sObject>{
public   String query;
public set<id> userSetId{get;set;}
Public   Set<Id> idSet = new Set<Id>();
public Map<id,id> UserManagerMaplist{get;set;}
public List<User> userList = new List<User>();

    
  global database.querylocator start(Database.BatchableContext BC){
   return Database.getQueryLocator(query);
 
  }
  global void execute(Database.BatchableContext BC, List<sObject> scope){ 
       List<Lead> leads= New List<Lead>();// List for Updating the Leads    
       
       for(sObject s : scope){ 
            Lead l= (Lead)s;  
            l.User_Manager__c =  UserManagerMaplist.get(l.ownerId); 
             leads.add(l);
             }
             
        if(leads.size()>0){
           try{
               Update leads;
               
           }
           catch(DMLException e){
               system.debug('DMLException'+e);
           }
        }
          
        
  }       
  global void finish(Database.BatchableContext BC){
      List<User> newUserSet = new List<User>();
      List<User> userSet = [SELECT Id,OORLeadManagerUpdate__c FROM User WHERE Id IN: userSetId];
      for(User userObj : userSet)
      {
        userObj.OORLeadManagerUpdate__c = false;
        newUserSet.add(userObj);
      }
        update newUserSet;
    }
  
        
 }